﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.SetUpScale;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.ScaleData
{
    /// <summary>
    /// 添加磅秤表数据
    /// </summary>
    public class AddScaleDataHandler : IRequestHandler<AddScaleDataCommand, Result>
    {
        public IBaseRepository<scaleContexts> ScaleContextRepository { get; set; }
        public IMapper mapper { get; set; }
        public AddScaleDataHandler(IBaseRepository<scaleContexts> scaleContextRepository, IMapper mapper)
        {
            ScaleContextRepository = scaleContextRepository;
            this.mapper = mapper;
        }
        public async Task<Result> Handle(AddScaleDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            var data = mapper.Map<scaleContexts>(request);
            if (data != null)
            {
                var AddNum = ScaleContextRepository.Add(data);
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            { 
                return result;
            }
        }
    }
}
